home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / cbrecali.c < prev    next >
Text File  |  1990-06-20  |  3KB  |  137 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbrecali.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stdlib.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13. #include <btree.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      cbrecalign - align cbase record cursor
  22.  
  23. SYNOPSIS
  24.      #include <cbase.h>
  25.  
  26.      int cbrecalign(cbp, field)
  27.      cbase_t *cbp;
  28.      int field;
  29.  
  30. DESCRIPTION
  31.      The cbrecalign function aligns the record cursor in cbase cbp
  32.      with the key cursor for the specified field.  Other cursors are
  33.      not affected.
  34.  
  35.      cbrecalign will fail if one or more of the following is true:
  36.  
  37.      [EINVAL]       cbp is not a valid cbase pointer.
  38.      [EINVAL]       field is not a valid field number for
  39.                     cbase cbp.
  40.      [CBELOCK]      cbp is not locked.
  41.      [CBENKEY]      field is not a key.
  42.      [CBENOPEN]     cbp is not open.
  43.  
  44. SEE ALSO
  45.      cbkeyalign, cbrecfirst, cbreclast, cbrecnext, cbrecprev.
  46.  
  47. DIAGNOSTICS
  48.      Upon successful completion, a value of 0 is returned.  Otherwise,
  49.      a value of -1 is returned, and errno set to indicate the error.
  50.  
  51. ------------------------------------------------------------------------------*/
  52. int cbrecalign(cbp, field)
  53. cbase_t *cbp;
  54. int field;
  55. {
  56.     void *buf = NULL;
  57.     cbrpos_t cbrpos = NIL;
  58.     lspos_t lspos = NIL;
  59.  
  60.     /* validate arguments */
  61.     if (!cb_valid(cbp)) {
  62.         errno = EINVAL;
  63.         return -1;
  64.     }
  65.  
  66.     /* check if not open */
  67.     if (!(cbp->flags & CBOPEN)) {
  68.         errno = CBENOPEN;
  69.         return -1;
  70.     }
  71.  
  72.     /* validate arguments */
  73.     if (field < 0 || field >= cbp->fldc) {
  74.         errno = EINVAL;
  75.         return -1;
  76.     }
  77.  
  78.     /* check if field not a key */
  79.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  80.         errno = CBENKEY;
  81.         return -1;
  82.     }
  83.  
  84.     /* check if not locked */
  85.     if (!(cbp->flags & CBLOCKS)) {
  86.         errno = CBELOCK;
  87.         return -1;
  88.     }
  89.  
  90.     /* check if record cursor is null */
  91.     if (btcursor(cbp->btpv[field]) == NULL) {
  92.         /* set record cursor to null */
  93.         if (lssetcur(cbp->lsp, NULL) == -1) {
  94.             CBEPRINT;
  95.             return -1;
  96.         }
  97.         errno = 0;
  98.         return 0;
  99.     }
  100.  
  101.     /* allocate storage for key */
  102.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  103.         CBEPRINT;
  104.         errno = CBEPANIC;
  105.         return -1;
  106.     }
  107.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  108.     if (buf == NULL) {
  109.         CBEPRINT;
  110.         errno = ENOMEM;
  111.         return -1;
  112.     }
  113.  
  114.     /* get key */
  115.     if (btgetk(cbp->btpv[field], buf) == -1) {
  116.         CBEPRINT;
  117.         free(buf);
  118.         return -1;
  119.     }
  120.  
  121.     /* get record position and place in key */
  122.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  123.     lspos = cbrpos;
  124.  
  125.     /* position record cursor */
  126.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  127.         CBEPRINT;
  128.         return -1;
  129.     }
  130.  
  131.     free(buf);
  132.     buf = NULL;
  133.  
  134.     errno = 0;
  135.     return 0;
  136. }
  137.